package cn.psvmc.cxzapi.controller.system;

import cn.psvmc.cxzapi.model.common.*;
import cn.psvmc.cxzapi.model.system.TPlatformMsg;
import cn.psvmc.cxzapi.response.*;
import cn.psvmc.cxzapi.service.system.TPlatformMsgService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 平台消息管理
 * @Author auto
 * @Date 2024-08-15
 */
@RestController
@RequestMapping("/platform_msg")
public class TPlatformMsgController {

    @Resource
    private TPlatformMsgService tPlatformMsgService;

    /**
    * @Author auto 2024-08-15 新增平台消息表
    * @Param tPlatformMsg 平台消息表
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody TPlatformMsg tPlatformMsg) {
        ToolUtils.validation(new Object[]{
                tPlatformMsg.getTitle(), "标题", 2
                , tPlatformMsg.getMsgType(), "类型", 1
                , tPlatformMsg.getSummary(), "概要", 2
        });
        tPlatformMsgService.add(tPlatformMsg);
        return ResultGenerator.genSuccessResult("保存成功！");
    }

    /**
    * @Author auto 2024-08-15 更新平台消息表
    * @Param tPlatformMsg 平台消息表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TPlatformMsg tPlatformMsg) {
        ToolUtils.validation(new Object[]{tPlatformMsg.getPmid(), "id", 1, tPlatformMsg.getRversion(), "版本", 1});
        int updateResult = tPlatformMsgService.edit(tPlatformMsg);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
    * @Author auto 2024-08-15 查询平台消息表详情
    * @Param entity
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/detail")
    public Result detail(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tPlatformMsgService.detail(id));
    }

    /**
    * @Author auto 2024-08-15 查询平台消息表列表
    * @Param entity 分页查询通用实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/list")
    public PageResult2 list(@RequestBody SearchEntity entity) {
        Page<TPlatformMsg> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tPlatformMsgService.list(queryPage, entity));
    }

    /**
     * @Description 我的消息列表
     * @Date 2024/8/15 21:44
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_my")
    public PageResult2 listMy(@RequestBody SearchEntity entity) {
        Page<TPlatformMsg> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tPlatformMsgService.listMy(queryPage, entity));
    }

    /**
     * @Description 阅读消息
     * @Date 2024/8/15 21:57
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/read")
    public Result read(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "消息用户id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tPlatformMsgService.read(id));
    }

    /**
     * @Description 删除消息
     * @Date 2024/8/21 11:29
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/delete")
    public Result delete(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        Integer rversion = entity.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        tPlatformMsgService.delete(id, rversion);
        return ResultGenerator.genSuccessResult("删除成功！");
    }

    /**
     * @Description 上架
     * @Date 2025/1/19 21:36
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/up")
    public Result up(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        tPlatformMsgService.updateMsgStatus(id, 1);
        return ResultGenerator.genSuccessResult("上架成功！");
    }

    /**
     * @Description 下架
     * @Date 2025/1/19 21:36
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/down")
    public Result down(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        tPlatformMsgService.updateMsgStatus(id, 2);
        return ResultGenerator.genSuccessResult("下架成功！");
    }
}
